package com.ft.scan.common;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * 阿里云 OSS 工具类
 */
public class AliOSSUtils {
    // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
    private static final String ENDPOINT = "https://oss-cn-wuhan-lr.aliyuncs.com";
    // 填写AccessKey ID和AccessKey Secret
    private static final String ACCESS_KEY_ID = "LTAI5t7ZT8AdxAYQjgKPAeyJ";
    private static final String ACCESS_KEY_SECRET = "5aGKAGnBf0vqF4mi6Q6TPTBi8ewygZ";
    // 填写Bucket名称，例如examplebucket。
    private static final String BUCKET_NAME = "scan-kjq";

    /**
     * 实现上传图片到OSS
     *
     * @param multipartFile 上传的文件
     * @return 返回上传成功的对象名，失败返回null
     */
    public static String uploadFileToOSS(MultipartFile multipartFile) {
        // 创建CredentialsProvider对象
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, credentialsProvider);

        try {
            // 获取上传文件的文件名
            String fileOriginalName = multipartFile.getOriginalFilename();
            // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
            String objectName = "scan/" + fileOriginalName;
            // 上传文件至OSS
            ossClient.putObject(BUCKET_NAME, objectName, new ByteArrayInputStream(multipartFile.getBytes()));
            return "https://scan-kjq.oss-cn-wuhan-lr.aliyuncs.com/" + objectName;
        } catch (OSSException oe) {
            System.err.println("上传文件到OSS失败，OSSException信息：" + oe.getErrorCode() + " - " + oe.getErrorMessage());
        } catch (ClientException ce) {
            System.err.println("上传文件到OSS失败，ClientException信息：" + ce.getMessage());
        } catch (IOException e) {
            System.err.println("上传文件到OSS失败，IOException信息：" + e.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        // 如果上传失败返回 null
        return null;
    }

    public static String uploadFileToOSS(String fileOriginalName, byte[] msg) {
        // 创建CredentialsProvider对象
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, credentialsProvider);

        try {
            // 获取上传文件的文件名

            // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
            String objectName = "scan/" + fileOriginalName;
            // 上传文件至OSS
            ossClient.putObject(BUCKET_NAME, objectName, new ByteArrayInputStream(msg));
            return "https://scan-kjq.oss-cn-wuhan-lr.aliyuncs.com/" + objectName;
        } catch (OSSException oe) {
            System.err.println("上传文件到OSS失败，OSSException信息：" + oe.getErrorCode() + " - " + oe.getErrorMessage());
        } catch (ClientException ce) {
            System.err.println("上传文件到OSS失败，ClientException信息：" + ce.getMessage());
        } catch (Exception e) {
            System.err.println("上传文件到OSS失败，IOException信息：" + e.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        // 如果上传失败返回 null
        return null;
    }

}
